'use strict'

const parse = require('./xmlBody')

function formatRet(obj) {
  if (!obj) {
    return null;
  }

  if (!obj.xml) {
    return null;
  }

  return obj.xml;
}

module.exports = (options) => {
  return function * (next) {
    const ctx = this
    if (ctx.is('*/xml')) {
      switch (ctx.method) {
        case 'POST':
        case 'PUT':
        case 'PATCH':
        case 'TRACE':
        var ret = yield parse(ctx.req, options)
        ctx.xmlBody = formatRet(ret);
        break
      }
    }
    yield next
  }
}
